package com.zc.controller;

import com.zc.entity.Bug;
import com.zc.entity.Needs;
import com.zc.entity.User;
import com.zc.mapper.ItemMapper;
import com.zc.mapper.NeedsMapper;
import com.zc.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class NeedsController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    NeedsMapper needsMapper;
    @Autowired
    ItemMapper itemMapper;
    @GetMapping("/doaddneeds")
    public  String doaddneeds(Model model, HttpSession session)
    {
        int teamid = (int) session.getAttribute("teamid");
        List userlist=userMapper.selectByTeamId(teamid);
        model.addAttribute("userlist",userlist);
        return "need/addneeds";
    }
    @ResponseBody
    @PostMapping("/needadd")
    public  String needadd(Needs need,HttpSession session)
    {
        int teamid = (int) session.getAttribute("teamid");
        int newid = (int) session.getAttribute("userid");
        int itemid=itemMapper.selectByTeamId(teamid).getId();
        need.setItemid(itemid);
        need.setNewdata(new Date());
        need.setStatus("1");
        need.setNewid(newid);
        needsMapper.insert(need);
        return "success";
    }
    @ResponseBody
    @GetMapping("/needslist/{status}")
    public  List needslist(@PathVariable("status") Integer status, HttpSession session)
    {
        int teamid = (int) session.getAttribute("teamid");
        int itemid=itemMapper.selectByTeamId(teamid).getId();
        List<Needs> Needs=null;
        if(status==0)
        {
            Needs=needsMapper.selectByitemId(itemid);
        }else {
            Needs=needsMapper.selectByitemIdAndGrade(itemid,status);
        }

        return Needs;
    }
    @GetMapping("/needweihu")
    public String needweihu(@Param("id")int id,
                            Model model,HttpSession session){
        int teamid = (int) session.getAttribute("teamid");
        List userlist=userMapper.selectByTeamId(teamid);
        model.addAttribute("userlist",userlist);
        Needs need=needsMapper.selectByPrimaryKey(id);
        model.addAttribute("need",need);
        return "need/needweihu";
    }
    @PutMapping("/needweihu")
    public String userUpdate1(Needs needs)
    {
        needs.setNewdata(new Date());
        needsMapper.updateByPrimaryKeySelective(needs);
        return "need/needslist";
    }
    @ResponseBody
    @GetMapping("/guanbineed/{id}")
    public  String guanbineed(@PathVariable("id") Integer id, HttpSession session)
    {
       Needs needs=new Needs();
       needs.setId(id);
       needs.setStatus("4");
        needsMapper.updateByPrimaryKeySelective(needs);
        return "SUCCESS";
    }
    @ResponseBody
    @GetMapping("/kfneedslist/{status}")
    public  List kfneedslist(@PathVariable("status") Integer status, HttpSession session)
    {
        int teamid = (int) session.getAttribute("teamid");
        int userid = (int) session.getAttribute("userid");
        int itemid=itemMapper.selectByTeamId(teamid).getId();
        List<Needs> Needs=null;
        if(status==0)
        {
            Needs=needsMapper.selectByitemId(itemid);
        }else {
            Needs=needsMapper.selectByitemIdAnddownid(itemid,userid);
        }

        return Needs;
    }
    @ResponseBody
    @GetMapping("/qrneed/{id}")
    public  String qrneed(@PathVariable("id") Integer id, HttpSession session)
    {
        Needs needs=new Needs();
        needs.setId(id);
        needs.setStatus("2");
        needsMapper.updateByPrimaryKeySelective(needs);
        return "SUCCESS";
    }
    @ResponseBody
    @GetMapping("/wcneed/{id}")
    public  String wcneed(@PathVariable("id") Integer id, HttpSession session)
    {
        Needs needs=new Needs();
        needs.setId(id);
        needs.setStatus("3");
        needs.setEnddata(new Date());
        needsMapper.updateByPrimaryKeySelective(needs);
        return "SUCCESS";
    }
}
